import { createApp } from 'vue'
import './style.scss'
import App from './App.vue'
import router from './router/index'
import { createPinia } from 'pinia'
import { Http } from '@/utils/http'
import components from '@/components/index'
import i18n from '@/i18n/index'
import 'element-plus/dist/index.css'
import 'element-plus/theme-chalk/display.css'
import ElementPlus from 'element-plus'
import directive from '@/directive'
// import '@/plugin/last-commit-msg/toggle.js'
import { mockRuqest } from './mock'
import { renderWithQiankun, qiankunWindow } from "vite-plugin-qiankun/dist/helper"
import actions from './utils/qiankun-action'

// console.log('-------------vue-app main.ts-------------')
Http.init()

if(import.meta.env.MODE === 'mock') {
	mockRuqest()
}

let app: any = null

const render = (props?: any) => {
	const { container } = props ?? {}

	const store = createPinia()
	app = createApp(App)
	
	app.use(ElementPlus)
	app.use(store)
	app.use(router)
	app.use(components)
	app.use(i18n)
	app.use(directive)
	// app.mount('#app')
	app?.mount(container ? container.querySelector('#container') : '#container');
  	// console.log("开始加载子组件 app");
}

renderWithQiankun({
	async mount(props: any) {
	//   await props.onGlobalStateChange((state: any) => {
	// 	console.log("子应用接收的参数", state);
	// 	state.publicPath && window.localStorage.setItem("mainJumpPublicPath", state.publicPath);
	//   }, true);
	//   actions.setActions(props);
	//   await actions.onGlobalStateChange((state: any) => {
    //     console.log('sub app onmouted', state)
    // }, true)
		// await actions.setGlobalState({isUnMountSubApp: false})
		render(props);
	},
	bootstrap(): any {
	  console.log("%c", "color:green;", " ChildOne bootstrap");
	  return new Promise((resolve, reject) => {
		resolve('bootstrap')
	  })
	},
	update() {
	  console.log("%c", "color:green;", " ChildOne update");
	},
	unmount(props: any) {
	  console.log("sub app vue3 unmount", props);
	  app.unmount();
	  app._container.innerHTML = "";
	  app = null;
	//   window.location.reload()
	}
});

// 判断是否是 qiankun 渲染
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
	// __webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__;
	// console.log('不是 qiankun 渲染', qiankunWindow)
	render()
}
